package com.sxpi.zixun.service.questions;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.QuestionsBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.QuestionsMapper;
import com.sxpi.zixun.pojo.Questions;
import com.sxpi.zixun.vo.QuestionsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/12 12:51
 */
@Service
@Slf4j
public class IQuestionsServiceImpl extends ServiceImpl<QuestionsMapper,Questions> implements IQuestionsService{

    /**
     * 查询文章管理列表信息
     * @return
     */
    @Override
    public List<QuestionsBo> findQuestionsList() {
        LambdaQueryWrapper<Questions> questionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2：查询发布的 0 未发布  1 发布
        questionsLambdaQueryWrapper.eq(Questions::getStatus,1);
        questionsLambdaQueryWrapper.eq(Questions::getIsdelete,0);
        List<Questions> list = this.list(questionsLambdaQueryWrapper);
        return tranferListBo(list,QuestionsBo.class);
    }

    /**
     * 查询文章管理列表信息并分页
     * @param questionsVo
     * @return
     */
    @Override
    public IPage<QuestionsBo> findQuestionsPage(QuestionsVo questionsVo) {
        Page<Questions> questionsPage = new Page<>(questionsVo.getPageNo(), questionsVo.getPageSize());
        LambdaQueryWrapper<Questions> questionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 排除字段
        questionsLambdaQueryWrapper.select(Questions.class,colum-> !colum.getColumn().equals("content"));
        // 根据关键词搜索信息
        questionsLambdaQueryWrapper.and(Vsserts.isNotEmpty(questionsVo.getKeyword()),wrapper -> wrapper
                .like(Questions::getTitle,questionsVo.getKeyword())
                .or()
                // 根据关键词搜索信息
                .like(Questions::getCategorytitle,questionsVo.getKeyword())
                .or()
                // 根据关键词搜索信息
                .like(Questions::getNickname,questionsVo.getKeyword()));
        // 根据分类查询
        questionsLambdaQueryWrapper.eq(Vsserts.isNotNull(questionsVo.getCategoryId()), Questions::getCategoryid,questionsVo.getCategoryId());
        //查询发布的 0 未发布  1 发布
        questionsLambdaQueryWrapper.eq(questionsVo.getStatus() != null ,Questions::getStatus,questionsVo.getStatus());
        // 根据分类查询
        questionsLambdaQueryWrapper.eq(Vsserts.isNotNull(questionsVo.getCategoryId()), Questions::getCategorycid,questionsVo.getCategoryId());
        // 根据关键词搜索信息
        questionsLambdaQueryWrapper.like(Vsserts.isNotEmpty(questionsVo.getKeyword()), Questions::getCategoryctitle,questionsVo.getKeyword());
        // 根据时间排降序
        questionsLambdaQueryWrapper.orderByDesc(Questions::getCreateTime);
        Page<Questions> page = this.page(questionsPage, questionsLambdaQueryWrapper);
        return tranferPageBo(page,QuestionsBo.class);
    }

    /**
     * 根据文章管理id查询明细信息
     * @param id
     * @return
     */
    @Override
    public QuestionsBo getQuestionsById(Long id) {
        Questions questions = this.getById(id);
        return tranferBo(questions,QuestionsBo.class);
    }

    /**
     * 保存和修改文章管理
     * @param questions
     * @return
     */
    @Override
    public QuestionsBo saveUpdateQuestions(Questions questions) {
        boolean flag = this.saveOrUpdate(questions);
        return flag ? tranferBo(questions,QuestionsBo.class)  : null;
    }

    /**
     * 根据文章管理id删除文章管理
     * @param id
     * @return
     */
    @Override
    public int deleteQuestionsById(Long id) {
        Questions adminRole = this.getById(id);
        if (adminRole != null) {
            Questions questions2 = new Questions();
            questions2.setId(id);
            questions2.setIsdelete(1);
            boolean b = this.updateById(questions2);
            return 1;
        }
        return 0;
    }

    /**
     * 根据文章管理ids批量删除文章管理
     * @param batchIds
     * @return
     */
    @Override
    public boolean delBatchQuestions(String batchIds) {
        try{
            String[] split = batchIds.split(",");
            List<Questions> questionsList = Arrays.stream(split).map(idsStr -> {
                Questions questions = new Questions();
                questions.setId(new Long(batchIds));
                questions.setIsdelete(1);
                return questions;
            }).collect(Collectors.toList());
            return this.updateBatchById(questionsList);
        }catch (Exception exception){
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }


}
